package com.virgo.demo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.exception.IApplicationException;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.demo.system.converter.AdminRoleConverter;
import com.virgo.demo.system.domain.AdminRoleDo;
import com.virgo.demo.system.domain.AdminUserDo;
import com.virgo.demo.system.domain.bo.AdminRoleBo;
import com.virgo.demo.system.domain.vo.AdminRoleVo;
import com.virgo.demo.system.mapper.AdminRoleMapper;
import com.virgo.demo.system.mapper.AdminUserMapper;
import com.virgo.demo.system.service.AdminRoleService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRoleDo> implements AdminRoleService {

	private final AdminRoleConverter adminRoleConverter;

	private final AdminRoleMapper adminRoleMapper;

	private final AdminUserMapper adminUserMapper;

	/**
	 * 根据角色名称查找
	 * @param name String
	 * @return AdminRoleDo
	 */
	@Override
	public AdminRoleDo loadByName(String name) {
		LambdaQueryWrapper<AdminRoleDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AdminRoleDo::getName,name);
		queryWrapper.last(" limit 1 ");
		return getOne(queryWrapper);
	}

	/**
	 * 编辑角色
	 * @param adminRoleBo AdminRoleBo
	 */
	@Override
	public void saveAdminRole(AdminRoleBo adminRoleBo) {
		AdminRoleDo exist = loadByName(adminRoleBo.getName());
		AdminRoleDo adminRoleDo = new AdminRoleDo();
		BeanUtils.copyProperties(adminRoleBo,adminRoleDo);
		if (!ObjectUtils.isEmpty(exist) && !exist.getId().equals(adminRoleDo.getId())) {
			throw new IApplicationException("角色-" + adminRoleDo.getName() + "已存在");
		}
		if (ObjectUtils.isEmpty(adminRoleDo.getId()) || adminRoleDo.getId().compareTo(0L) < 1) {
			save(adminRoleDo);
			return;
		}
		updateById(adminRoleDo);
	}

	/**
	 * 修改状态
	 * @param id     Long
	 * @param status Integer
	 */
	@Override
	public void updateStatus(Long id, Integer status) {
		AdminRoleDo adminRoleDo = new AdminRoleDo();
		adminRoleDo.setId(id);
		adminRoleDo.setStatus(status);
		updateById(adminRoleDo);
	}

	/**
	 * 删除角色
	 * @param ids String
	 */
	@Override
	public void deleteAdminRole(String ids) {
		List<Long> idList = Arrays.asList(ICommonUtils.stringToArray(ids, ","));
		LambdaQueryWrapper<AdminUserDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(AdminUserDo::getRoleId, idList);
		long userNum = adminUserMapper.selectCount(queryWrapper);
		if (userNum > 0L) {
			throw new IApplicationException("角色存在关联用户，不能删除");
		}
		removeByIds(idList);
 	}

	/**
	 * 角色详情
	 * @param id Long
	 * @return AdminRoleVo
	 */
	@Override
	public AdminRoleVo findInfo(Long id) {
		return adminRoleConverter.convert(getById(id), AdminRoleVo.class);
	}

	/**
	 * 角色列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<AdminRoleVo>
	 */
	@Override
	public ListDataVo<AdminRoleVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<AdminRoleDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(AdminRoleDo::getId);

		//不查询权限节点
		queryWrapper.select(Arrays.asList(
				 AdminRoleDo::getId
				,AdminRoleDo::getName
				,AdminRoleDo::getStatus
				,AdminRoleDo::getCreatedAt
				,AdminRoleDo::getUpdatedAt
		));

		String name = IRequestUtils.getString(request,"name","");
		if (!ObjectUtils.isEmpty(name)) {
			queryWrapper.like(AdminRoleDo::getName,name);
		}
		String status = IRequestUtils.getString(request,"status","");
		if ("1".equals(status)) {
			queryWrapper.eq(AdminRoleDo::getStatus,1);
		} else if ("0".equals(status)) {
			queryWrapper.eq(AdminRoleDo::getStatus,0);
		}
		Page<AdminRoleDo> page = new Page<>(currentPage, pageSize);
		IPage<AdminRoleDo> iPage = adminRoleMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),adminRoleConverter.convert(iPage.getRecords(), AdminRoleVo.class));
	}
}